package com.das.mechanic_base.widget;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.groundpush.X3TimeAdapter;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.bean.ground.GetDateTimeBean;
import com.das.mechanic_base.bean.main.AppointClueBean;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.utils.X3DateUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.umeng.analytics.MobclickAgent;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class X3BottomTimeAppointDialog extends X3BaseBottomSheetDialog implements View.OnClickListener, CalendarView.OnMonthChangeListener, CalendarView.OnCalendarSelectListener, CalendarView.OnWeekChangeListener, X3MemberButtonView.IOnClickAffirm {

    private TextView tv_cancel;
    private TextView tv_date;
    private RecyclerView rlv_time;
    private X3MemberButtonView btn_sure;
    private CalendarView cv_view;
    private ImageView iv_left;
    private ImageView iv_right;
    private Button btn_back;
    private RelativeLayout rl_show;

    private String inputDate;
    private X3TimeAdapter timeAdapter;
    private String today;
    private AppointClueBean clueBean;

    private String carTime;
    private List<GetDateTimeBean> searchNumBean=new ArrayList<>();

    public X3BottomTimeAppointDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        tv_cancel = getView(R.id.tv_cancel);
        btn_sure = getView(R.id.btn_sure);
        rlv_time = getView(R.id.rlv_time);
        tv_date = getView(R.id.tv_date);
        cv_view = getView(R.id.cv_view);
        iv_left = getView(R.id.iv_left);
        iv_right = getView(R.id.iv_right);
        btn_back = getView(R.id.btn_back);
        rl_show = getView(R.id.rl_show);

        rlv_time.setLayoutManager(new LinearLayoutManager(mContext));
        tv_cancel.setOnClickListener(this);
        btn_sure.setiOnClickAffirm(this);
        iv_right.setOnClickListener(this);
        iv_left.setOnClickListener(this);
        btn_back.setOnClickListener(this);
        rl_show.setOnClickListener(this);
        //当前日期第一种样式
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");//如果用到别的就用特定
        //字符表示，如星期F
        today = sdf.format(d);
        //当前日期第二中样式
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");//如果用到别的就用特定
        String toDayTwo=sdf2.format(d);

        tv_date.setText(X3DateUtils.MonthWithYear(cv_view.getCurYear() + "-" + cv_view.getCurMonth() + "-01 00:00:00"));

        inputDate = cv_view.getCurYear() + "-" + (cv_view.getCurMonth() < 10 ? "0"+cv_view.getCurMonth() : cv_view.getCurMonth()) + "-" +(cv_view.getCurDay() < 10 ? "0"+cv_view.getCurDay()  : cv_view.getCurDay());
        cv_view.setOnMonthChangeListener(this);
        cv_view.setOnCalendarSelectListener(this);
        cv_view.setOnWeekChangeListener(this);

        rlv_time.setLayoutManager(new GridLayoutManager(mContext,2));
        timeAdapter = new X3TimeAdapter(mContext);
        rlv_time.setAdapter(timeAdapter);
        timeAdapter.setiOnClickGroup(ageRange -> {
            btn_sure.changeBtnStatus(true);
            carTime = ageRange;
        });
        getDate(toDayTwo);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_appoint_time_bottom_dialog;
    }

    private void getDate(String data){
        NetWorkHttp.getApi().getAppointTimeRange2(data)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .subscribe(new HttpCallBack<List<GetDateTimeBean>>() {
                    @Override
                    protected void onDone(List<GetDateTimeBean> searchNumBeans) {
                        searchNumBean=searchNumBeans;
                        if (timeAdapter != null) {
                            timeAdapter.setChangeGroup(searchNumBean);
                        }
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    @Override
    public void show() {
        super.show();
        btn_sure.changeBtnStatus(false);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_cancel || id == R.id.btn_back) {
            dismiss();
        } else if (id == R.id.iv_left) {
            if (cv_view != null) {
                cv_view.scrollToPre();
            }
        } else if (id == R.id.iv_right) {
            if (cv_view != null) {
                cv_view.scrollToNext();
            }
        }
    }

    IOnClickTime iOnClickTime;

    public void setiOnClickTime(IOnClickTime iOnClickTime) {
        this.iOnClickTime = iOnClickTime;
    }

    @Override
    public void onWeekChange(List<Calendar> weekCalendars) {
    }

    @Override
    public void iOnClickAffirm() {
        if (X3StringUtils.isEmpty(carTime)) {
            btn_sure.reset();
//            ToastUtils.showMessage("未选择任何时间段~");
            return;
        }
        //这里截取字符串使用的中午-， 这里要主要和adapter的要统一，不然截取不到会报异常
        String[] split = carTime.split("—");
        String startTime = split[0].trim() + "";
        String endTime = split[1].trim() + "";

        if (clueBean == null) {
            btn_sure.reset();
            //ToastUtils.showMessage("车辆数据不完全，无法预约到店~");
            return;
        }
        clueBean.setAspirationDate(inputDate);
        clueBean.setAspirationTimeStr(startTime + "-" + endTime);

        if (iOnClickTime != null) {
            iOnClickTime.iOnClickTimeAppoint(clueBean);
        }
    }

    public interface IOnClickTime {
        void iOnClickTimeAppoint(AppointClueBean clueBean);
    }

    @Override
    public void onMonthChange(int year, int month) {
        tv_date.setText(X3DateUtils.MonthWithYear(year + "-" + month + "-01 00:00:00"));
    }

    @Override
    public void onCalendarOutOfRange(Calendar calendar) {

    }

    @Override
    public void onCalendarSelect(Calendar calendar, boolean isClick) {
        inputDate = calendar.getYear() + "-" + (calendar.getMonth() < 10 ? "0"+calendar.getMonth() : calendar.getMonth()) + "-" +(calendar.getDay() < 10 ? "0"+calendar.getDay():calendar.getDay());
        carTime = "";

        btn_sure.changeBtnStatus(false);
        getDate(inputDate);
    }

    /**
     * 选择完车辆之后
     * @param clue
     */
    public void setAppointClue(AppointClueBean clue) {
        clueBean = clue;
    }

    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("服务预约弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("服务预约弹窗");
    }
}
